//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmVPPStepCondition.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Methods/Finitedifferences/Utilities/FdmInnerValueCalculator.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
using namespace Cephei::QL::Methods::Finitedifferences::Utilities;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::CFdmVPPStepCondition (Double heatRate, Double pMin, Double pMax, UInt64 tMinUp, UInt64 tMinDown, Double startUpFuel, Double startUpFixCost, Double carbonPrice, UInt64 stateDirection, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ gasPrice, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ sparkSpreadPrice) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CFdmMesher^ _Cmesher;
    CFdmInnerValueCalculator^ _CgasPrice;
    CFdmInnerValueCalculator^ _CsparkSpreadPrice;
    try
    {
#ifdef HANDLE
        _phFdmVPPStepCondition = NULL;
#endif
        QuantLib::Real _heatRate = (QuantLib::Real)ValueHelper::Convert (heatRate); //d
        QuantLib::Real _pMin = (QuantLib::Real)ValueHelper::Convert (pMin); //d
        QuantLib::Real _pMax = (QuantLib::Real)ValueHelper::Convert (pMax); //d
        QuantLib::Size _tMinUp = (QuantLib::Size)ValueHelper::Convert (tMinUp); //d
        QuantLib::Size _tMinDown = (QuantLib::Size)ValueHelper::Convert (tMinDown); //d
        QuantLib::Real _startUpFuel = (QuantLib::Real)ValueHelper::Convert (startUpFuel); //d
        QuantLib::Real _startUpFixCost = (QuantLib::Real)ValueHelper::Convert (startUpFixCost); //d
        QuantLib::Real _carbonPrice = (QuantLib::Real)ValueHelper::Convert (carbonPrice); //d
        QuantLib::Size _stateDirection = (QuantLib::Size)ValueHelper::Convert (stateDirection); //d
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _CgasPrice = safe_cast<CFdmInnerValueCalculator^> (gasPrice);
        _CgasPrice->Lock();
        boost::shared_ptr<QuantLib::FdmInnerValueCalculator>& _gasPrice = static_cast<boost::shared_ptr<QuantLib::FdmInnerValueCalculator>&> (_CgasPrice->GetShared ()); 
        _CsparkSpreadPrice = safe_cast<CFdmInnerValueCalculator^> (sparkSpreadPrice);
        _CsparkSpreadPrice->Lock();
        boost::shared_ptr<QuantLib::FdmInnerValueCalculator>& _sparkSpreadPrice = static_cast<boost::shared_ptr<QuantLib::FdmInnerValueCalculator>&> (_CsparkSpreadPrice->GetShared ()); 
        _ppFdmVPPStepCondition = new boost::shared_ptr<QuantLib::FdmVPPStepCondition> (new QuantLib::FdmVPPStepCondition ( _heatRate,  _pMin,  _pMax,  _tMinUp,  _tMinDown,  _startUpFuel,  _startUpFixCost,  _carbonPrice,  _stateDirection,  _mesher,  _gasPrice,  _sparkSpreadPrice ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_CgasPrice != nullptr) _CgasPrice->Unlock();
        if (_CsparkSpreadPrice != nullptr) _CsparkSpreadPrice->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::CFdmVPPStepCondition (boost::shared_ptr<QuantLib::FdmVPPStepCondition>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmVPPStepCondition = NULL;
#endif
	_ppFdmVPPStepCondition = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::CFdmVPPStepCondition (QuantLib::FdmVPPStepCondition& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmVPPStepCondition = NULL;
#endif
	_ppFdmVPPStepCondition = new boost::shared_ptr<QuantLib::FdmVPPStepCondition> (&childNative);
    
    _FdmVPPStepConditionOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::CFdmVPPStepCondition (CFdmVPPStepCondition^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmVPPStepCondition = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmVPPStepCondition = new boost::shared_ptr<QuantLib::FdmVPPStepCondition> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::CFdmVPPStepCondition (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmVPPStepCondition = NULL;
#endif
	if (!t->IsSubclassOf(CFdmVPPStepCondition::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::CFdmVPPStepCondition (QuantLib::Handle<QuantLib::FdmVPPStepCondition>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmVPPStepCondition = &childNative;
	_ppFdmVPPStepCondition = &static_cast<boost::shared_ptr<QuantLib::FdmVPPStepCondition>>(childNative.currentLink());
    
    _FdmVPPStepConditionOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::CFdmVPPStepCondition (QuantLib::Handle<QuantLib::FdmVPPStepCondition> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmVPPStepCondition = &childNative;
	_ppFdmVPPStepCondition = &static_cast<boost::shared_ptr<QuantLib::FdmVPPStepCondition>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::CFdmVPPStepCondition (QuantLib::FdmVPPStepCondition childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmVPPStepCondition = NULL;
#endif
	_ppFdmVPPStepCondition = new boost::shared_ptr<QuantLib::FdmVPPStepCondition> (new QuantLib::FdmVPPStepCondition (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::~CFdmVPPStepCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmVPPStepCondition != NULL)
    {
	    delete _ppFdmVPPStepCondition;
        _ppFdmVPPStepCondition = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::!CFdmVPPStepCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmVPPStepCondition != NULL)
    {
	    delete _ppFdmVPPStepCondition;
    }
}
QuantLib::FdmVPPStepCondition& Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::GetReference ()
{
    if (_ppFdmVPPStepCondition == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmVPPStepCondition;
}
boost::shared_ptr<QuantLib::FdmVPPStepCondition>& Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::GetShared ()
{
    if (_ppFdmVPPStepCondition == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmVPPStepCondition;
}
QuantLib::FdmVPPStepCondition* Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::GetPointer ()
{
    if (_ppFdmVPPStepCondition == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmVPPStepCondition;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmVPPStepCondition>& Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::GetHandle ()
{
	if (_phFdmVPPStepCondition == NULL)
	{
		_phFdmVPPStepCondition = new Handle<QuantLib::FdmVPPStepCondition> (*_ppFdmVPPStepCondition);
	}
	return *_phFdmVPPStepCondition;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::HasNative () 
{
	return (_ppFdmVPPStepCondition != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdmVPPStepCondition^ Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition::ApplyTo (Cephei::QL::Math::IArray^ a, Double t)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	(*_ppFdmVPPStepCondition)->applyTo ( _a,  _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmVPPStepCondition^ Cephei::QL::Experimental::Finitedifferences::CFdmVPPStepCondition_Factory::Create (Double heatRate, Double pMin, Double pMax, UInt64 tMinUp, UInt64 tMinDown, Double startUpFuel, Double startUpFixCost, Double carbonPrice, UInt64 stateDirection, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ gasPrice, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ sparkSpreadPrice)
{
    return REFNEW CFdmVPPStepCondition ( heatRate,  pMin,  pMax,  tMinUp,  tMinDown,  startUpFuel,  startUpFixCost,  carbonPrice,  stateDirection,  mesher,  gasPrice,  sparkSpreadPrice);
}
